package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LibrarySession {

    protected static Book[] books;
    protected static Movies[] movie;
    protected static User[] users;
    private static boolean isLibrarianLoggedIn = false;
    private static boolean isUserLoggedIn = false;
    public InputAsker asker = new InputAsker();

    public LibrarySession() {

    }

    public static void setLibrarianLoggedIn(boolean value) {
        isLibrarianLoggedIn = value;
    }

    public static void setUserLoggedIn(boolean value) {
        isUserLoggedIn = value;
    }

    public LibrarySession(Book[] books1, Movies[] movie1, User[] users1) {
        books = books1;
        movie = movie1;
        users = users1;
        System.out.println("Hello");
        System.out.println("Welcome to Biblioteca");
        System.out.println();
        System.out.println();
    }

    public void displayMenu() {
        System.out.println("1. View all books");
        System.out.println("2. Login");
        System.out.println("3. Reserve a book for collection");
        System.out.println("4. Return a book");
        System.out.println("5. View movie ratings");
        System.out.println("6. Check your membership details");
        System.out.println("7. Log Out");
        System.out.println("8. Exit");
        System.out.println("Please select a option from the above list:  ");
    }

    public String acceptInput () {
        BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
        try {
            String input = bufferRead.readLine();
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean menu(int menuChoice)    {

        if (menuChoice < 1 && menuChoice > 7) {
        System.out.println("Select a valid option!");
        System.out.println("Please enter choice again...");
        return false;
    }
        if (menuChoice == 1) {
            for (int i = 0; i<5; i++)
                books[i].display();
        }
        else if (menuChoice == 2) {
            acceptUserIdAndPassword();
        }
        else if(menuChoice == 3) {
            acceptUserIdAndPassword();
            int bookId = Integer.parseInt(asker.ask("Please enter the Book Id : "));
            returnBook(bookId);
            borrowBook(bookId);

        }
        else if (menuChoice == 4) {
            acceptUserIdAndPassword();
            int bookid = Integer.parseInt(asker.ask("Please enter the Book Id : "));
            returnBook(bookid);
        }
        else if (menuChoice == 5) {
            System.out.println("Name\t\tYear\tDirector\t\tRating");
            for (int i = 0; i < 5; i++)
                movie[i].display();
        }
        else if (menuChoice == 6) {
            acceptUserIdAndPassword();
            if (isLibrarianLoggedIn) {
                users[0].display();
            } else {
                System.out.println("Please consult a librarian...");
            }
        }
        else if (menuChoice == 7) {
            UserLogIN.logOut();
        }
        else if (menuChoice == 8) {
            return true;
        }
        return true;

    }

    public void acceptUserIdAndPassword() {
        if (!isUserLoggedIn) {
            UserLogIN.logIn(asker.ask("Please enter your User Id : "),
                asker.ask("Please enter your Password : "));
        }
        else System.out.println("A user is already logged in!");
    }

    public boolean borrowBook(int bookId) {

        if (bookId<=books.length && bookId >0 ) {
            return books[bookId-1].setAvailabilityForBorrow();
        }
        return false;
    }

    public boolean returnBook(int bookid) {

        if (bookid<=books.length && bookid >0 ) {
            return books[bookid-1].setAvailabilityForReturn();
        }
        return false;
    }
}
